/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.com.apresentacao.pedagogico;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import src.com.negocio.pedagogico.CadastrarIdioma;
import src.com.negocio.pedagogico.CadastrarNivelIdioma;
import src.com.negocio.pedagogico.ConsultarTurma;

/**
 *
 * @author Giane
 */
public class ConsultaTurma extends javax.swing.JFrame {

    /**
     * Creates new form ConsultaTurma
     */
    public ConsultaTurma() throws SQLException {
        initComponents();
        idioma.setEnabled(false);
        nivel.setEnabled(false);
        prof.setEnabled(false);

        CadastrarIdioma c = new CadastrarIdioma();
        CadastrarNivelIdioma a = new CadastrarNivelIdioma();
        for (int i = 0; i < a.consultaNivel().size(); i++) {
            nivel.addItem(a.consultaNivel().get(i));
        }

        for (int i = 0; i < c.consultaIdioma().size(); i++) {
            idioma.addItem(c.consultaIdioma().get(i));
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jInternalFrame1 = new javax.swing.JInternalFrame();
        jLabel1 = new javax.swing.JLabel();
        codTurma = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        idioma = new javax.swing.JComboBox();
        nivel = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        prof = new javax.swing.JTextField();
        pesquisar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jRadioButton1 = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jInternalFrame1.setTitle("Consultar - Turma");
        jInternalFrame1.setVisible(true);

        jLabel1.setText("Código da Turma: ");

        codTurma.setName("codTurma"); // NOI18N

        jLabel2.setText("Idioma: ");

        jLabel3.setText("Nível: ");

        idioma.setModel(new javax.swing.DefaultComboBoxModel(new String[] { " ", " " }));
        idioma.setName("idioma"); // NOI18N

        nivel.setModel(new javax.swing.DefaultComboBoxModel(new String[] { " ", " " }));
        nivel.setName("nivel"); // NOI18N

        jLabel4.setText("Professor: ");

        prof.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                profActionPerformed(evt);
            }
        });

        pesquisar.setText("Pesquisar");
        pesquisar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                pesquisarMouseClicked(evt);
            }
        });
        pesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pesquisarActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null}
            },
            new String [] {
                "Turma", "Idioma", "Professor"
            }
        ));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jTable1MouseEntered(evt);
            }
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jRadioButton1.setSelected(true);
        jRadioButton1.setText("Pesquisar pelo código");
        jRadioButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jRadioButton1MouseClicked(evt);
            }
        });
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
        jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
        jInternalFrame1Layout.setHorizontalGroup(
            jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jInternalFrame1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jInternalFrame1Layout.createSequentialGroup()
                            .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel3))
                                .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(codTurma)
                                .addComponent(idioma, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(nivel, 0, 198, Short.MAX_VALUE)
                                .addComponent(prof))
                            .addGap(18, 18, 18)
                            .addComponent(jRadioButton1))
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jInternalFrame1Layout.createSequentialGroup()
                        .addComponent(pesquisar)
                        .addContainerGap())))
        );
        jInternalFrame1Layout.setVerticalGroup(
            jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jInternalFrame1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(codTurma, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jRadioButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(idioma, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(nivel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(prof, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pesquisar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 314, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jInternalFrame1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 1, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jInternalFrame1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void profActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_profActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_profActionPerformed

    private void pesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pesquisarActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_pesquisarActionPerformed

    private void pesquisarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_pesquisarMouseClicked
        String codigo = codTurma.getText().toString();
        String i_idioma = idioma.getSelectedItem().toString();
        String n_nivel = nivel.getSelectedItem().toString();
        String professor = prof.getText().toString();

        ConsultarTurma D = new ConsultarTurma();
        if (jRadioButton1.isSelected()) {
            try {
                ArrayList<String> result_turma = D.consultaTurma(codigo, "", "", "");
                if (!result_turma.isEmpty()) {
                    for (int i = 0; i < result_turma.size(); i++) {
                        jTable1.setValueAt(result_turma.get(i), 0, i);
                        //System.out.print(result_turma.get(i));
                    }
                } else {
                    javax.swing.JDialog inf = new javax.swing.JDialog();
                    inf.setSize(250, 150);
                    javax.swing.JOptionPane.showMessageDialog(inf, "A turma informa não existe!", "Informação", javax.swing.JOptionPane.WARNING_MESSAGE);

                }


            } catch (SQLException ex) {
                Logger.getLogger(ConsultaTurma.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            try {
                ArrayList<String> result_turma = D.consultaTurma("", i_idioma, n_nivel, professor);

                if (!result_turma.isEmpty()) {
                    jTable1.setModel(new javax.swing.table.DefaultTableModel(new String[]{"Turma", "Idioma", "Professor"}, result_turma.size() / 3));

                    //System.out.print(result_turma);
                    //for (int i = 0; i < result_turma.size(); i++) {
                    int i = 0;
                    for (int j = 0; j < result_turma.size() / 3; j++) {
                        for (int z = 0; z < 3; z++) {
                            jTable1.setValueAt(result_turma.get(i), j, z);
                            i++;
                        }
                    }
                }else {
                    javax.swing.JDialog inf = new javax.swing.JDialog();
                    inf.setSize(250, 150);
                    javax.swing.JOptionPane.showMessageDialog(inf, "A turma informado não existe!", "Informação", javax.swing.JOptionPane.WARNING_MESSAGE);

                }
                //}
            } catch (SQLException ex) {
                Logger.getLogger(ConsultaTurma.class.getName()).log(Level.SEVERE, null, ex);
            }



        }

    }//GEN-LAST:event_pesquisarMouseClicked

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jRadioButton1ActionPerformed

    private void jRadioButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jRadioButton1MouseClicked
        // TODO add your handling code here:
        if (!jRadioButton1.isSelected()) {
            codTurma.setEnabled(false);
            idioma.setEnabled(true);
            nivel.setEnabled(true);
            prof.setEnabled(true);
        } else {
            codTurma.setEnabled(true);
            idioma.setEnabled(false);
            nivel.setEnabled(false);
            prof.setEnabled(false);
        }
    }//GEN-LAST:event_jRadioButton1MouseClicked

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked


        try {
            String cod_turma = jTable1.getValueAt(jTable1.getSelectedRow(), 0).toString();
            CadastroTurma n = new CadastroTurma();
            n.setTurma(cod_turma);
            n.initValue();
            n.setVisible(true);
        } catch (SQLException ex) {
            Logger.getLogger(ConsultaTurma.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_jTable1MouseClicked

    private void jTable1MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseEntered
        // TODO add your handling code here:
    }//GEN-LAST:event_jTable1MouseEntered

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ConsultaTurma.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ConsultaTurma.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ConsultaTurma.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ConsultaTurma.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new ConsultaTurma().setVisible(true);
                } catch (SQLException ex) {
                    Logger.getLogger(ConsultaTurma.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField codTurma;
    private javax.swing.JComboBox idioma;
    private javax.swing.JInternalFrame jInternalFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JComboBox nivel;
    private javax.swing.JButton pesquisar;
    private javax.swing.JTextField prof;
    // End of variables declaration//GEN-END:variables
}
